package com.gateway.web.controller;

import com.gateway.api.domain.vo.MenuOrder;
import com.gateway.api.util.CacheUtil;
import com.gateway.api.util.IPUtil;
import com.gateway.api.util.LoginUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: shaoyu1
 * @date: 2019/11/6 18:00
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    CacheUtil cacheUtil;

//    MenuOrderService menuOrderService;

    private String NO_CHECK_PASSWORD = "!Q@W#E$R%T^Y&U*I(O)P)O(I*U&Y^T%R$E#W@Q!";

    @RequestMapping("/login")
    public String login(HttpServletRequest req,ModelMap modelMap){
        //有账号 说明已经登陆了
        String username = LoginUtil.getUsername(req);
        if(StringUtils.isNotEmpty(username)){
            return index(username,NO_CHECK_PASSWORD,modelMap,req);
        }
        return "login";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest req){
        LoginUtil.logout(req);
        return "login";
    }

    @RequestMapping("/main")
    public String index(String username,String password,ModelMap modelMap,HttpServletRequest req){
        String cacheUsername = LoginUtil.getUsername(req);
        if(StringUtils.isNotEmpty(cacheUsername)){
            username = cacheUsername;
            password = NO_CHECK_PASSWORD;
        }
        String msg = checkUser(req,username,password);
        if(StringUtils.isNotEmpty(msg)){
            modelMap.put("message",msg);
            return "login";
        }
        LoginUtil.checkLogin(req);
        setModelInfo(username,modelMap);
        return "index";
    }

    private void setModelInfo(String username, ModelMap modelMap) {
        List<MenuOrder> orders = new ArrayList<>();
        MenuOrder order = new MenuOrder();
        order.setId("1");
        order.setHref("");
        order.setTitle("测试菜单");
        List<MenuOrder> childrenOrders = new ArrayList<>();
        MenuOrder childrenOrder1 = new MenuOrder();
        childrenOrder1.setHref("/apiInfo/index");
        childrenOrder1.setTitle("API设置");
        childrenOrder1.setId("2");
        childrenOrders.add(childrenOrder1);
        order.setChildren(childrenOrders);
        orders.add(order);
        MenuOrder order2 = new MenuOrder();
        order2.setTitle("百度");
        order2.setHref("http://www.baidu.com");
        order2.setId("3");
        orders.add(order2);
        modelMap.put("orders",orders);
        MenuOrder frist = getfristMenu(orders);
        modelMap.put("fristMenu",frist);
        modelMap.put("testString","阿拉啦");
    }

    private MenuOrder getfristMenu(List<MenuOrder> orders) {
        for (MenuOrder order:orders) {
            if(CollectionUtils.isNotEmpty(order.getChildren())){
                return order.getChildren().get(0);
            }
            return order;
        }
        MenuOrder childrenOrder1 = new MenuOrder();
        childrenOrder1.setHref("/apiInfo/index");
        childrenOrder1.setTitle("API设置");
        childrenOrder1.setId("2");
        return childrenOrder1;
    }


    private String checkUser(HttpServletRequest req, String username, String password) {
        if(NO_CHECK_PASSWORD.equals(password)){
            //说明是从登录页过来的 已经登陆了
            return "";
        }
        if(StringUtils.isEmpty(username)){
            return "您还未登录!";
        }
        if(!LoginUtil.checkUser(req,username,password)){
            return "用户名或者密码错误!";
        }
        return "";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }
}
